package com.recipe.controller;

import com.recipe.common.entity.Result;
import com.recipe.entity.Dish;
import com.recipe.entity.RecommendDish;
import com.recipe.entity.User;
import com.recipe.entity.vo.RankVo;
import com.recipe.service.DishService;
import com.recipe.service.impl.Recommend;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * (Dish)表控制层
 *
 * @author lzk
 * @since 2024-06-17 15:23:24
 */
@RestController
@RequestMapping("dish")
@Slf4j
@RequiredArgsConstructor
public class DishController {


    private final DishService dishService;


    @Resource
    Recommend recommend;

    @GetMapping("/getAll")
    public Result<List<Dish>> getAll() {

        List<Dish> dishList = dishService.getAll();
        return Result.ok(dishList);
    }


    @GetMapping("/getContributeList")
    public Result<List<RankVo>> getContributeList() {
        if (log.isInfoEnabled()) {
            log.info("DishController.getContributeList");
        }
        List<RankVo> contributeList = dishService.getContributeList();
        return Result.ok(contributeList);
    }



    @GetMapping("/getPersonDish")
    public Result<List<Dish>> getPersonDish(@RequestParam String username) {
        if (log.isInfoEnabled()) {
            log.info("DishController.getPersonDish:{}", username);
        }
        List<Dish> dishList = dishService.getPersonDish(username);
        return Result.ok(dishList);
    }

    @GetMapping("/recommend")
    public Result<RecommendDish> recommend(@RequestParam String keyword) {

        if (log.isInfoEnabled()) {
            log.info("DishController.recommend:{}",keyword);
        }
        if(keyword!=null){
            RecommendDish recommendDish = recommend.recommendRecipe(keyword);

            return Result.ok(recommendDish);
        }else{
            return Result.fail();
        }

    }

    @GetMapping("/getBySource")
    public Result<List<Dish>> getByResource(String dishSource) {
        if (log.isInfoEnabled()) {
            log.info("DishController.getByResource:{}", dishSource);
        }
        List<Dish> dishList = dishService.getBySource(dishSource);
        return Result.ok(dishList);

    }

    @GetMapping("/getById/{id}")
    public Result<Dish> getById(@PathVariable Long id) {
        if (log.isInfoEnabled()) {
            log.info("DishController.getById:{}", id);
        }
        Dish dish = dishService.getDishById(id);
        return Result.ok(dish);
    }

    @GetMapping("/getDetailDish/{id}")
    public Result<Dish> getDetailDish(@PathVariable Long id) {
        if (log.isInfoEnabled()) {
            log.info("DishController.getDetailDish:{}", id);
        }
        Dish dish = dishService.getDetailDish(id);
        return Result.ok(dish);
    }

    @GetMapping("/getauthor/{userid}")
    public Result<User> getauthor(@PathVariable Long userid) {
        if (log.isInfoEnabled()) {
            log.info("DishController.getauthor:{}", userid);
        }
        User user = dishService.getauthor(userid);
        return Result.ok(user);
    }

    @PostMapping("/adddish")
    public Result<Dish> adddish(@RequestBody Dish dish) {
        if (log.isInfoEnabled()) {
            log.info("DishController.adddish:{}", dish);
        }
        dishService.add(dish);
        return Result.ok();
    }

    @PostMapping("updatedish")
    public Result<Dish> updatedish(@RequestBody Dish dish) {
        if (log.isInfoEnabled()) {
            log.info("DishController.updatedish:{}", dish);
        }
        dishService.updateById(dish);
        return Result.ok();
    }
}

